package com.gdatacloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.context.request.async.DeferredResult;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicates;

import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {

	 @Bean(value = "defaultApi2")
	    public Docket defaultApi2() {
	        Docket docket=new Docket(DocumentationType.SWAGGER_2)
	                .apiInfo(productApiInfo())
	                //分组名称
	                .groupName("1.X版本")
	                .select()
	                //这里指定Controller扫描包路径
	                .apis(RequestHandlerSelectors.basePackage("com.gdatacloud.flow"))
	                .paths(PathSelectors.any())
	                .build();
	        return docket;
	 }
	@Bean
	public Docket ProductApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.genericModelSubstitutes(DeferredResult.class)
				.useDefaultResponseMessages(false)
				.forCodeGeneration(false)
				.pathMapping("/z/")
				.select()
				.apis(RequestHandlerSelectors.any())// 对所有api进行监控
				.paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控 
				.build()
				.apiInfo(productApiInfo());
	}

	private ApiInfo productApiInfo() {
		return new ApiInfoBuilder()
				.title("物联网开放平台接口文档")
				.description("简单优雅的restfun风格")
				.termsOfServiceUrl("https://baidu.com")
				.version("1.1")
				.build();
	}
}